package com.glory.eapequipmentmonitor.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.activity.PackPackageActivity;
import com.glory.eapequipmentmonitor.adapter.PackListAdapter;
import com.glory.eapequipmentmonitor.bean.PackBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NotDoneFragment extends FreeUI_BaseFragment {

    public PackListAdapter packListAdapter;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.llMain)
    FrameLayout llMain;
    @BindView(R.id.checkAll)
    CheckBox checkAll;

    public NotDoneFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_not_done;
    }

    @Override
    protected void initView() {
        rvThingList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(getActivity(), 1));

        packListAdapter = new PackListAdapter(null);
        packListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        packListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(packListAdapter);
    }

    @Override
    protected void initData() {
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                packListAdapter.setAll(isChecked);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @OnClick(R.id.tvPack)
    public void onViewClicked() {
        final List<PackBean> selectList = new ArrayList<>();
        for (PackBean packBean : packListAdapter.getData()) {
            if (packBean.isSelect()) {
                selectList.add(packBean);
            }
        }
        if (selectList.size() == 0) {
            showToast("请选择要包装的批次", false);
            return;
        }
        packListAdapter.clearPos();
        packListAdapter.getData().removeAll(selectList);
        showToast("包装成功", true);
        ((PackPackageActivity) getActivity()).setAlreadyDoneList(selectList);
        packListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            ((PackPackageActivity) getActivity()).etLotId.setEnabled(true);
        }
    }
}
